//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imagemanager.util;

public class LocationMediaFilter extends MediaFilter {
    private double mRadius;
    private double mCenterLat;
    private double mCenterLon;
    public static final int EARTH_RADIUS_METERS = 6378137;
    public static final int LAT_MIN = -90;
    public static final int LAT_MAX = 90;
    public static final int LON_MIN = -180;
    public static final int LON_MAX = 180;

    LocationMediaFilter(double centerLatitude, double centerLongitude, double thresholdRadius) {
        this.mCenterLat = centerLatitude;
        this.mCenterLon = centerLongitude;
        this.mRadius = thresholdRadius;
    }

    LocationMediaFilter(double latitude1, double longitude1, double latitude2, double longitude2) {
        this.mCenterLat = centerLat(latitude1, latitude2);
        this.mCenterLon = centerLon(longitude1, longitude2);
        this.mRadius = distanceBetween(latitude1, longitude1, latitude2, longitude2);
    }

    public static final double centerLat(double lat1, double lat2) {
        return centerOfAngles(lat1, lat2, 90);
    }

    public static final double centerLon(double lon1, double lon2) {
        return centerOfAngles(lon1, lon2, 180);
    }

    private static final double centerOfAngles(double ang1, double ang2, int wrapAroundThreshold) {
        boolean wrapAround = false;
        if(Math.abs(ang1 - ang2) > (double)wrapAroundThreshold) {
            wrapAround = true;
        }

        double center = (ang1 + ang2) * 0.5D;
        if(wrapAround) {
            center += (double)wrapAroundThreshold;
            center %= (double)wrapAroundThreshold;
        }

        return center;
    }

    public static double distanceBetween(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2.0D) * Math.sin(dLat / 2.0D) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2.0D) * Math.sin(dLon / 2.0D);
        double c = 2.0D * Math.atan2(Math.sqrt(a), Math.sqrt(1.0D - a));
        return 6378137.0D * c;
    }

    public static final double toKm(double meter) {
        return meter / 1000.0D;
    }

    public static final double toMile(double meter) {
        return meter / 1609.0D;
    }

    public boolean pass(MediaItem item) {
        double radius = distanceBetween(this.mCenterLat, this.mCenterLon, item.mLatitude, item.mLongitude);
        return radius <= this.mRadius;
    }
}
